﻿using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Awsc.DataAcquisitionSys.Core.Model;
using Awsc.DataAcquisitionSys.Core.Models.POCO;

namespace Awsc.DataAcquisitionSys.Core.Databases.Configurations
{
    internal class EntityTypeConfigurationBase<T> : IEntityTypeConfiguration<T> where T : EntityBase
    {
        public virtual void Configure(EntityTypeBuilder<T> builder)
        {
            builder.HasKey(p => p.Id);
            builder.Property(a => a.Id).ValueGeneratedOnAdd();
            builder.Property(p => p.Name).IsRequired().HasMaxLength(256);
        }
    }
    internal class MissionsConfig : EntityTypeConfigurationBase<Mission>
    {
        public override void Configure(EntityTypeBuilder<Mission> builder)
        {
            builder.ToTable("Missions");
            builder.Property(p => p.ManagerName).IsRequired(false).HasMaxLength(256);
            builder.Property(p => p.Remarks).IsRequired(false);
            builder.Property(p => p.TeamID).IsRequired(false).HasMaxLength(256);
            builder.Property(p => p.Unit).IsRequired(false).HasMaxLength(256);
            builder.Property(p => p.Location).IsRequired(false);
            builder.Property(p => p.ConstructionLocation).IsRequired(false);
            builder.Property(p => p.WellDeep).IsRequired(false).HasMaxLength(256);
            builder.Property(p => p.ManulBottomHole).IsRequired(false).HasMaxLength(256);
            builder.Property(p => p.FillTime).IsRequired(false);
            builder.Property(p => p.WellMode).IsRequired(false).HasMaxLength(256);
            builder.Property(p => p.WellType).IsRequired(false).HasMaxLength(256);
            builder.Property(p => p.DesignLayer).IsRequired(false).HasMaxLength(256);
            builder.Property(p => p.CurrentLayer).IsRequired(false).HasMaxLength(256);
            builder.Property(p => p.WorkType).IsRequired(false).HasMaxLength(256);
            builder.Property(p => p.DeviceMode).IsRequired(false).HasMaxLength(256);
            builder.Property(p => p.WorkingProcedure).IsRequired(false).HasMaxLength(256);
            builder.Property(p => p.WellParkerMode).IsRequired(false).HasMaxLength(256);
            builder.Property(p => p.BopConfiguration).IsRequired(false).HasMaxLength(256);
            builder.Property(p => p.LatestPresureTest).IsRequired(false).HasMaxLength(256);
            builder.Property(p => p.CreateTime).IsRequired(false);
            builder.Property(p => p.LatestPresureTestTime).IsRequired(false);
            builder.Property(p => p.FinishTime).IsRequired(false);
            builder.Ignore(p => p.IsFinished);
            builder.Ignore(p => p.SensorDataTableName);
            base.Configure(builder);
        }
    }

    internal class AlarmRuleConfig : EntityTypeConfigurationBase<AlarmRuleModel>
    {
        public override void Configure(EntityTypeBuilder<AlarmRuleModel> builder)
        {
            builder.ToTable("AlarmRules");
            builder.Property(p => p.Level);
            builder.Property(p => p.RuleType);
            builder.Property(p => p.IsBuildIn);
            builder.Property(p => p.AlarmMethods);
            builder.Property(p => p.Condition).IsRequired(false);
            builder.Property(p => p.Description).IsRequired(false);
            base.Configure(builder);
        }
    }

    internal class WearPartConfig : EntityTypeConfigurationBase<WearPart>
    {
        public override void Configure(EntityTypeBuilder<WearPart> builder)
        {
            builder.ToTable("WearParts");
            builder.Property(p => p.Description).IsRequired(false);
            builder.Property(p => p.Status);
            builder.Property(p => p.LastMaintenanceDate).IsRequired(false);
            builder.Property(p => p.UsageCount);
            builder.Property(p => p.FactoryCount);
            builder.Ignore(p => p.UsagePercent);
            base.Configure(builder);
        }
    }
    internal class SentinelConfig : EntityTypeConfigurationBase<Sentinel>
    {
        public override void Configure(EntityTypeBuilder<Sentinel> builder)
        {
            builder.ToTable("Sentinels");
            builder.Property(p => p.StartTime).IsRequired(false);
            builder.Property(p => p.EndTime).IsRequired(false);
            base.Configure(builder);
        }
    }
}
